<?php
session_start();
require_once("common/config.inc.php");
$login = false;
if(!isset($_SESSION["user"])){
	header("Location: user/UserLogin.php");
    exit();
}
?>
<?php include("common/db.inc.php"); ?>
<?php	
global $currentJournal,$divDisplayStyle,$operateResult,$currentDate;
	init();		
	
	function init(){
		$GLOBALS[divDisplayStyle]=array();
		$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:none";
		$operation=$_GET["operation"];
		if($operation!=null && $operation=="download"){			
			$id=$_GET["id"];
			if($id==null || $id==""){				
				$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:inline";
				$GLOBALS[operateResult]="Journal ID is blank!";			
			}else{
				$GLOBALS[currentJournal]=getJournal($id);
				if($GLOBALS[currentJournal]==null || $GLOBALS[currentJournal]==""){
					//id not found
					$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:inline";
					$GLOBALS[operateResult]="Journal ID not found!";						
				}else{		
					
				    //check validate date	
					$userid=$_SESSION["user"];
					$flag=checkUserValidate($userid);
					if($flag==2){
						$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:inline";
						$GLOBALS[operateResult]="User validate date expired!<br/><br/>Please contact <a href='mailto:medc@dicp.ac.cn'>administrator</a>.";
						return;
					}else if($flag==3){
						$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:inline";
						$GLOBALS[operateResult]="The journal is beyond your validate time periond!<br/><br/>Please contact <a href='mailto:medc@dicp.ac.cn'>administrator</a>.";
						return;
					}											    			    
					$flag=downloadJournal($GLOBALS[currentJournal]);
					//file not found
					if($flag==-1){
						$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:inline";
						$GLOBALS[operateResult]="Journal file not found!<br/><br/>Please contact <a href='mailto:medc@dicp.ac.cn'>administrator</a>.";
					}else{			
						$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:none";	
						header("JournalShow.php");
						exit();
					}
				}
			}
		}else{
			$GLOBALS[divDisplayStyle]["operateResultDiv"]="dispaly:inline";
			$GLOBALS[operateResult]="Invalid operation!";	

		}
				
	}
	
	function getJournal($id){
		$sql = "SELECT * FROM medcdb.t_journal where id='$id'";
		$connection=getMEDCConnection();
		$dataArray=executeQuery($sql,$connection);
		return $dataArray[0];
	}
	
	function downloadJournal($journalObj){
		//$columnNames=array("id","name","facepic","storename","originalname","createdate","brief");
		$storename=$journalObj["storename"];
		$originalname=$journalObj["originalname"];		
		//check file exists 
		if (!file_exists($storename)) {
			return -1;
		}
		set_time_limit ( 0 );
		header ( "Pragma: public" );
		header ( "Expires: 0" );
		header ( "Content-Encoding: none" );
		header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
		header ( "Cache-Control: public" );
		header ( "Content-Description: File Transfer" );
		header ( "Content-Disposition: attachment; filename=".$originalname );
		header ( "Content-Transfer-Encoding: binary" );
		header ( "Content-Length:".filesize ( $storename) );
		readfile ( $storename );
	}	
	
	function checkUserValidate($userid){
		$currentDate=date("Y-m-d H:i:s");//yyyy-MM-dd,2006-07-11 09:32:12
		//check if user expired
		$sql = "SELECT * FROM medcdb.t_user where userid='$userid' and valto>='$currentDate'";
		$connection=getMEDCConnection();
		$userArray=executeQuery($sql,$connection);	
		if(count($userArray)<=0){
			return 2;//expired
		}
		//check if user can download the journal
		$userRecord=$userArray[0];
		$valfrom=$userRecord["valfrom"];
		$valto=$userRecord["valto"];
		$journalId=$_GET["id"];
		$sql = "SELECT * FROM medcdb.t_journal where id='$journalId' and createdate>='$valfrom' and createdate<='$valto'";
		$userArray=executeQuery($sql,$connection);	
		if(count($userArray)<=0){
			return 3;//expired
		}		
		return 1;
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="" />
<meta name="Description" content="" />
<meta name="author" content="" />
<title>Journal Download</title>
<link rel="stylesheet" type="text/css" href="./css/medc.css" />
<script language="JavaScript" type="text/javaScript" src="./js/medc_common.js"></script>
<script language="JavaScript" type="text/javascript">	
</script>
</head>

<body>
<!--Top area-->
	<div id="toparea">
		<?php include "common/header.inc.php"?>
	</div>
	<div id="menu">
		<?php include "user/UserMenu.inc.php";?>
	</div>	

<div id="mainarea">
<form action="" method="post" name="medcform">
<div id="operateResultDiv" style="<? echo $GLOBALS[divDisplayStyle]["operateResultDiv"]; ?>" >
	<div align="center">
	<p> <?php echo $GLOBALS[operateResult]; ?></p> 
	</div>
</div>
</form>
</div><!-- end of mainarea-->

	<div class="myclear"></div>
	<div id="footer">
		<?php include "common/footer.inc.php";?>
	</div>
</body>
</html>
